package com.oa.ch.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.oa.cc.service.EmpService;
import com.oa.ch.service.AttService;
import com.oa.domain.Attendance;
import com.oa.domain.Dept;
import com.oa.domain.Emp;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.media.sound.AlawCodec;

public class AttendanceAction extends ActionSupport {

	private AttService attService;
	private EmpService empService;
	private List<Attendance> atts;
	private Attendance att;
	private Date date;
	private Emp emp;
	private List<Dept> depts;
	private String year;
	private String month;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
	private Long empno;
	private Long deptno;

	/* 得到所有 考勤信息 */
	public String getAllAtts() {
		depts = attService.getDeptsByExample(new Dept());
		atts = attService.getAttByExample(new Attendance());
		/*
		 * for(Attendance a:atts){ Emp e = attService.getEmpById(a.getEmpno());
		 * a.setEmp(e); }
		 */
		for (int i = 0; i < atts.size(); i++) {
			Emp e = attService.getEmpById(atts.get(i).getEmpno());
			atts.get(i).setEmp(e);
		}
		return "atts";
	}

	/**
	 * 根据员工编号得到此员工的考勤信息
	 */
	public String getAttByEmpno() {
		att = new Attendance();
		att.setEmpno(empno);
		atts = attService.getAttByExample(att);
		depts = attService.getDeptsByExample(new Dept());
		for (Attendance a : atts) {
			Emp e = attService.getEmpById(a.getEmpno());
			a.setEmp(e);
		}
		return "attsByEmpno";
	}

	/**
	 * 得到所有部门信息
	 */
	public String getAllDepts() {

		depts = attService.getDeptsByExample(new Dept());
		return "depts";
	}

	/**
	 * 根据条件得到考勤信息
	 * 
	 * @throws ParseException
	 */

	public String getAttsByExp() throws Exception {
		date = sdf.parse(year + "-" + month);
		depts = attService.getDeptsByExample(new Dept());
		if(att.getEmpno()!=null&&att.getEmpno()>0){
			att.setEmpno(empno);
			atts = attService.getAttByExample(att);
		}
		else if (att.getEmp().getEname() != null
				&& !"".equals(att.getEmp().getEname())) {
			Emp e = new Emp();
			e = empService.getEmpsByExample(att.getEmp()).get(0);
			att.setEmpno(e.getEmpno());
			atts = attService.getAttByExample(att);
		} else if (deptno != null && deptno > 0) {
			// att.setAtttime(date);
			System.out.println("部门编号：" + deptno);
			atts = new ArrayList<Attendance>();
			List<Emp> list = new ArrayList<Emp>();
			list = empService.getEmpsByDeptno(deptno);
			System.out.println("部门员工数:" + list.size());
			for (Emp e : list) {
				List<Attendance> list2 = new ArrayList<Attendance>();
				att.setEmpno(e.getEmpno());
				list2 = attService.getAttByExample(att);
				for (Attendance a : list2) {
					atts.add(a);
				}
			}

		}
		for (Attendance a : atts) {
			Emp e = attService.getEmpById(a.getEmpno());
			a.setEmp(e);
		}
		return "getAttByExample";
	}

	/**
	 * 上班签到
	 * 
	 * @param attService
	 */
	public String up() {

		date = Calendar.getInstance().getTime();
		return "up";
	}

	public Attendance getAtt() {
		return att;
	}

	public void setAtt(Attendance att) {
		this.att = att;
	}

	public void setAttService(AttService attService) {
		this.attService = attService;
	}

	public List<Attendance> getAtts() {
		return atts;
	}

	public void setAtts(List<Attendance> atts) {
		this.atts = atts;
	}

	public Emp getEmp() {
		return emp;
	}

	public void setEmp(Emp emp) {
		this.emp = emp;
	}

	public List<Dept> getDepts() {
		return depts;
	}

	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

	public EmpService getEmpService() {
		return empService;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public void setEmpno(Long empno) {
		this.empno = empno;
	}

	public void setDeptno(Long deptno) {
		this.deptno = deptno;
	}
}
